package com.molichuxing.gateway.bff.dealer.services.impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.gateway.bff.dealer.convertor.Convert;
import com.molichuxing.gateway.bff.dealer.entity.response.StoreInfoVo;
import com.molichuxing.gateway.bff.dealer.entity.response.StoreDetailVo;
import com.molichuxing.gateway.bff.dealer.entity.response.StoreNameVo;
import com.molichuxing.gateway.bff.dealer.entity.response.StoreVo;
import com.molichuxing.gateway.bff.dealer.services.StoreBffService;
import com.molichuxing.gateway.property.VehicleMakeTypeEnum;
import com.molichuxing.services.business.dto.response.StoreInfoBizDto;
import com.molichuxing.services.business.service.GoodsBizService;
import com.molichuxing.services.business.service.StoreBizService;
import com.molichuxing.services.infrastructure.dto.response.DealerDto;
import com.molichuxing.services.infrastructure.dto.response.RegionDto;
import com.molichuxing.services.infrastructure.dto.response.StoreDto;
import com.molichuxing.services.infrastructure.dto.response.VehicleMakeDto;
import com.molichuxing.services.infrastructure.service.DealerService;
import com.molichuxing.services.infrastructure.service.RegionService;
import com.molichuxing.services.infrastructure.service.StoreService;
import com.molichuxing.services.infrastructure.service.VehicleMakeService;
import com.molichuxing.services.property.StoreStatusEnum;
import com.molichuxing.services.property.StoreVerifyStatusEnum;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("storeBffService")
public class StoreBffServiceImpl implements StoreBffService {
    private static final Logger logger = LoggerFactory.getLogger(StoreBffServiceImpl.class);

    @Resource
    private StoreBizService storeBizService;

    @Resource
    private RegionService regionService;

    @Resource
    private StoreService storeService;


    @Resource
    private GoodsBizService goodsBizService;

    @Resource
    private DealerService dealerService;

    @Resource
    private VehicleMakeService vehicleMakeService;

    /**
     * 城市获取门店(老版本接口)
     *
     * @param map key：regionId Integer 区域ID
     * @return
     * @throws Exception
     */
    @Override
    public List<StoreInfoVo> getStoreInfoList(Map<String, Object> map) throws Exception {
        String baiduCode = ObjectCastUtil.castString(map.get("baiduCode"));
        if (StringUtils.isNoneBlank(baiduCode)) {
            RegionDto regionDto = regionService.getCityByBaiduCode(baiduCode);
            if (null == regionDto) {
                throw new BizException("未找到对应城市");
            }

            Integer regionId = regionDto.getId();
            map.put("regionId", regionId);
        }

        List<Integer> statusList = new ArrayList<>();
        statusList.add(StoreStatusEnum.OPEN.getValue());
        statusList.add(StoreStatusEnum.SUSPENSION.getValue());
        map.put("statusList", statusList);
        map.put("verifyStatus", StoreVerifyStatusEnum.VERIFY_PASS.getValue());
        List<StoreInfoBizDto> list = storeBizService.getStoreInfoList(map);
        if (null == list || list.size() < 1) {
            return null;
        }

        return Convert.toStoreInfoVo(list);
    }

    /**
     * 城市获取门店
     *
     * @param map key：regionId Integer 区域ID
     * @return
     * @throws Exception
     */
    @Override
    public StoreDetailVo getStoreDetailList(Map<String, Object> map) throws Exception {
        String baiduCode = ObjectCastUtil.castString(map.get("baiduCode"));
        Integer regionId = ObjectCastUtil.castInteger(map.get("regionId"));
        if (null == regionId && StringUtils.isNoneBlank(baiduCode)) {
            RegionDto regionDto = regionService.getCityByBaiduCode(baiduCode);
            if (null == regionDto) {
                throw new BizException("未找到对应城市");
            }

            regionId = regionDto.getId();
        }

        List<Integer> statusList = new ArrayList<>();
        statusList.add(StoreStatusEnum.OPEN.getValue());
        statusList.add(StoreStatusEnum.SUSPENSION.getValue());
        map.put("statusList", statusList);
        map.put("verifyStatus", StoreVerifyStatusEnum.VERIFY_PASS.getValue());
        map.put("regionId", null);
        List<StoreInfoBizDto> list = storeBizService.getStoreInfoList(map);
        if (null == list || list.size() < 1) {
            return null;
        }

        List<StoreInfoVo> storeDetailVoList = Convert.toStoreInfoVo(list);
        StoreDetailVo storeInfoVo = new StoreDetailVo();
        Integer finalRegionId = regionId;
        List<StoreInfoVo> selectStoreList = storeDetailVoList.stream().filter(o -> o.getRegionId().equals(finalRegionId)).collect(Collectors.toList());
        List<StoreInfoVo> otherStoreList = storeDetailVoList.stream().filter(o -> !o.getRegionId().equals(finalRegionId)).collect(Collectors.toList());
        storeInfoVo.setSelectStoreList(selectStoreList);
        storeInfoVo.setOtherStoreList(otherStoreList);
        return storeInfoVo;
    }

    /**
     * 获取4s门店
     *
     * @param shortName
     * @return
     */
    @Override
    public List<StoreNameVo> getFoursStore(String shortName) {
       /* if (StringUtils.isBlank(shortName)) {
            return null;
        }*/
        List<StoreDto> storeDtoList = storeService.getFoursStore(shortName);
        return Convert.toStoreNameVo(storeDtoList);
    }

    @Override
    public StoreVo getStoreById(Integer storeId) throws Exception {
        StoreVo storeVo = Convert.toStoreVo(storeService.getStoreById(storeId));
        if (null != storeVo) {
            DealerDto dealerDto = dealerService.getDealerById(storeVo.getDealerId());
            if (null != dealerDto) {
                storeVo.setDealerStatus(dealerDto.getStatus());
            }
            VehicleMakeDto vehicleMakeDto = vehicleMakeService.getVehicleMakeDtoById(storeVo.getBrandId());
            if (null != vehicleMakeDto) {
                if (vehicleMakeDto.getName().contains(VehicleMakeTypeEnum.BQ.getName())) {
                    storeVo.setBrandType(VehicleMakeTypeEnum.BQ.getValue());
                } else if (vehicleMakeDto.getName().contains(VehicleMakeTypeEnum.CC.getName())) {
                    storeVo.setBrandType(VehicleMakeTypeEnum.CC.getValue());
                }
            }
        }
        return storeVo;
    }

    /**
     * @param storeId
     * @param goodsId
     * @return
     * @throws Exception
     */
    @Override
    public Integer checkGoodsStoreStatus(Integer storeId, Integer goodsId) throws Exception {
        StoreDto storeDto = storeService.getStoreById(storeId);
        if (null == storeDto) {
            throw new BizException("门店信息不存在");
        }
        if (storeDto.getStatus() != StoreStatusEnum.OPEN.getValue()) {
            return storeDto.getStatus();
        }
        Boolean result = goodsBizService.isProhibited(goodsId, storeDto.getDealerId());
        if (result) {
            return 4;
        }
        return 1;
    }


}
